package com.project.lk.nurseapp.common.view.bluetooth;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.media.MediaMetadataCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.SimpleViewSwitcher;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.ByteToStringUtils;
import com.project.lk.nurseapp.utils.ProgressUtils;
import com.project.lk.nurseapp.utils.SharedPreferencesUtils;
import com.project.lk.nurseapp.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 * 功能介绍：这是蓝牙设备详情界面
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class BluetoothDetailsActivity extends BaseActivity {


    @BindView(R.id.item_name)
    TextView itemName;
    @BindView(R.id.item_address)
    TextView itemAddress;
    @BindView(R.id.item_hint)
    TextView itemHint;
    @BindView(R.id.ble_bind_but)
    Button bleBindBut;
    @BindView(R.id.mProgressBar)
    SimpleViewSwitcher mProgressBar;
    private BleManager bleManager;
    private BlueTooth blueTooth;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void initData() {
        blueTooth = (BlueTooth) getIntent().getSerializableExtra("BlueTooth");
        bleManager = BleManager.init();
        bleManager.initialize();
        ProgressUtils.setProgress(mProgressBar);
    }

    @Override
    protected void initView() {
        if (blueTooth.getName()!=null) {
            itemName.setText(blueTooth.getName());
        }
        if (blueTooth.getAddress()!=null) {
            itemAddress.setText(blueTooth.getAddress());
        }

        itemHint.setVisibility(View.GONE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bluetooth_details;
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());  //注册广播
    }

    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (BleManager.ACTION_GATT_CONNECTED.equals(action)) {
            } else if (BleManager.ACTION_GATT_DISCONNECTED.equals(action)) {
            } else if (BleManager.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {  //智能ble蓝牙设备连接成功回调
                mProgressBar.setVisibility(View.GONE);
                if (blueTooth.getName().contains("JKFR")){  //温度计
                    SharedPreferencesUtils.setParam("Thermom",true);
                    ToastUtils.showShortToast("蓝牙连接成功");
                }else if (blueTooth.getName().contains("SZLSD SPPLE Module")){  //血压计
                    SharedPreferencesUtils.setParam("NewSphy",false);
                    SharedPreferencesUtils.setParam("Sphy",true);
                    ToastUtils.showShortToast("蓝牙连接成功");
                }else if (blueTooth.getName().contains("LD")){   //血压计
                    SharedPreferencesUtils.setParam("NewSphy",true);
                    SharedPreferencesUtils.setParam("Sphy",true);
                    ToastUtils.showShortToast("蓝牙连接成功");
                }else if (blueTooth.getName().contains("JKBGM")){  //血糖计
                    SharedPreferencesUtils.setParam("Glucose",true);
                    ToastUtils.showShortToast("蓝牙连接成功");
                }else if (blueTooth.getName().contains("MD1000AF4")){  //血糖计
                    SharedPreferencesUtils.setParam("Voice",true);
                    ToastUtils.showShortToast("蓝牙连接成功");
                }else {
                    ToastUtils.showShortToast("你连接的蓝牙不是指定的测量工具,测量不一定能成功");
                }
                AppManager.getAppManager().finishActivity(BluetoothDetailsActivity.class);
                AppManager.getAppManager().finishActivity(BluetoothListActivity.class);
            } else if (BleManager.ACTION_DATA_AVAILABLE.equals(action)) { //数据回调
//                Log.e("MLOG",intent.getStringExtra(BleManager.EXTRA_DATA));
            }
        }
    };



    private static IntentFilter makeGattUpdateIntentFilter() {  //注册广播
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleManager.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BleManager.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BleManager.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BleManager.ACTION_DATA_AVAILABLE);
        return intentFilter;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @OnClick({R.id.back, R.id.ble_bind_but})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.ble_bind_but:
                boolean connect = bleManager.connect(blueTooth.getAddress());  //连接蓝牙设备
                if (connect) {
                    mProgressBar.setVisibility(View.VISIBLE);
                }else {
                    ToastUtils.showShortToast("连接失败");
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mGattUpdateReceiver); //取消注册广播
    }
}
